package com.bird.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLDecoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bird.weibo.SendWeibo;

public class CreatePic extends HttpServlet {

	/**
	 * 这个servlet是用来创建图片的
	 */
	private static final long serialVersionUID = 1L;
	private static final int WIDTH = 450;  
    private static int HEIGHT = 35;  

	public void doGet(HttpServletRequest request, HttpServletResponse response){
	try{
		request.setCharacterEncoding("UTF-8");  
	    response.setContentType("text/html;charset=UTF-8");  
	    
		String text = request.getParameter("key");
		text=URLDecoder.decode(text, "UTF-8");
	    
		String title = request.getParameter("data");
		title=URLDecoder.decode(title, "UTF-8");

	    
		HEIGHT = text.length()*2;
		 //在内存中创建一个图片  
        BufferedImage image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_BYTE_GRAY);  
        Graphics g = image.getGraphics();//得到图形  
        
        //1.设置背景颜色 
        g.setColor(Color.WHITE);  
        g.fillRect(0, 0, WIDTH, HEIGHT);  
        
       
        
        //3.写文字
        g.setColor(Color.BLACK);  
        g.setFont(new Font("宋体",Font.PLAIN,14));  
       
        int i = 1;
        boolean BREAK = false;
    //    System.out.println(text);
        while(text!=null){
        	if(text.indexOf("<br/>")<=0){
        		while(text!=null){
        			
        			if(text.length()<=25){
            			g.drawString(text, 5, 20*(i));
            			BREAK = true;
                		break;
            		}else{
            			g.drawString(text.substring(0,25), 5, 20*i);
        	        	text = text.substring(25);
        	        	i++;
            		}
        			
        		}
        	}
        	
        	if(BREAK==true){
        		g.drawString("------------------------------------------------------", 5, 20*(++i));
        		g.setColor(Color.BLACK);  
        	    g.setFont(new Font("宋体",Font.PLAIN,12));  
        		g.drawString("来自小黄鸡微博版http://www.chenxianlu.cn  开发者 @bird1992", 5, 20*(++i));
        		break;
        	}
        //	System.out.println(text.indexOf("<br/>"));
	        String temp = text.substring(0,text.indexOf("<br/>"));
	        if(temp.length()<=25){
	        	g.drawString(temp, 5, 20*i);
	        }else{
	        	while(temp!=null){
		        	if(temp.length()<=25){
	        			g.drawString(temp, 5, 20*(i));
	        			break;
	        		}else{
	        			g.drawString(temp.substring(0,25), 5, 20*i);
	        			temp = temp.substring(25);
	    	        	i++;
	        		}
	        	}
	        }
	        text = text.substring(text.indexOf("<br/>")+5);
	        i++;
	    //    System.out.println(text);
        }
        
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       
       
    //   File file = new File("d:\\1.png");
       ImageIO.write(image, "png", out);
       SendWeibo send = new SendWeibo();
       send.sendWeibo(String.valueOf(request.getSession().getAttribute("token")), title, out.toByteArray());
       response.getWriter().write("长微博发送成功!!!");
 //      System.out.println("继续");
	}catch(Exception e){
	//	System.out.println("报错了");
		try {
			e.printStackTrace();
			response.getWriter().write("不要重复发送内容重复的微博，请更改微博标题或继续聊天后再试~");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}

	public void doPost(HttpServletRequest request, HttpServletResponse response){
		try{
			doGet(request, response);
		   
			}catch(Exception e){
			
			}
		}

}
